package ezy.widget.pullrefresh;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import ezy.widget.R;

public class PullHeader extends PullBase implements IPullIndicator {
    private static final int ROTATE_ANIM_DURATION = 150;

    private RelativeLayout mHeaderContainer;

    private ImageView mArrowImageView;
    private ProgressBar mProgressBar;
    private TextView mHintTextView;
    private TextView mHeaderTimeView;
    private TextView mHeaderTimeViewTitle;
    private Animation mRotateUpAnim;
    private Animation mRotateDownAnim;

    @Override
    public View onCreateView(ViewGroup parent) {
        View view = inflate(parent, R.layout.swipe_refresh_header);

//        view.setBackgroundColor(0xFFff0000);
        mHeaderContainer = findViewById(R.id.swipe_refresh_header_content);
        mArrowImageView = findViewById(R.id.swipe_refresh_header_arrow);
        mHintTextView = findViewById(R.id.swipe_refresh_header_hint);
        mProgressBar = findViewById(R.id.swipe_refresh_header_progressbar);
        mHeaderTimeView = findViewById(R.id.swipe_refresh_header_txt_update_time);
        mHeaderTimeViewTitle = findViewById(R.id.swipe_refresh_header_lbl_update_time);

        // 初始化旋转动画
        final float pivot = 0.5f;
        final float degree = -180f;
        final int self = Animation.RELATIVE_TO_SELF;
        mRotateUpAnim = new RotateAnimation(0.0f, degree, self, pivot, self, pivot);
        mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateUpAnim.setFillAfter(true);
        mRotateDownAnim = new RotateAnimation(degree, 0.0f, self, pivot, self, pivot);
        mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateDownAnim.setFillAfter(true);

        return view;
    }

    private boolean _isAutoHide = true;
    @Override
    public boolean isHide() {
        return _isAutoHide;
    }

    private PullRefreshList.State _state = PullRefreshList.State.NONE;
    private SimpleDateFormat _dateFormat = new SimpleDateFormat("更新于 MM-dd HH:mm:ss");
    @Override
    protected void onStateChanged(PullRefreshList.State state, PullRefreshList.State old) {
        _isAutoHide = state != PullRefreshList.State.LOADING;

        mArrowImageView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.INVISIBLE);
        getView().setVisibility(state != PullRefreshList.State.NORMAL ? View.VISIBLE : View.INVISIBLE);

        switch (state) {
        case NORMAL:
            if (_state == PullRefreshList.State.LOADING) {
                mHeaderTimeView.setText(_dateFormat.format(new Date()));
            }
            mArrowImageView.clearAnimation();
            mHintTextView.setText(R.string.swipe_refresh_header_pulling);
            break;
        case READY:
            mArrowImageView.clearAnimation();
            mArrowImageView.startAnimation(mRotateUpAnim);
            mHintTextView.setText(R.string.swipe_refresh_header_ready);
            break;
        case PULLING:
            if (PullRefreshList.State.READY == old) {
                mArrowImageView.clearAnimation();
                mArrowImageView.startAnimation(mRotateDownAnim);
            }

            mHintTextView.setText(R.string.swipe_refresh_header_pulling);
            break;
        case LOADING:
            mArrowImageView.clearAnimation();
            mArrowImageView.setVisibility(View.INVISIBLE);
            mProgressBar.setVisibility(View.VISIBLE);
            mHintTextView.setText(R.string.swipe_refresh_header_loading);
        default:
            break;
        }
        _state = state;
    }

    public void setUpdateTimeText(CharSequence value) {
        mHeaderTimeViewTitle.setVisibility(TextUtils.isEmpty(value) ? View.INVISIBLE : View.VISIBLE);
        mHeaderTimeView.setText(value);
    }

}
